﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Xml;
using System.Data;

using BLL;
using DAL;
using Models;

using Gift.DAL;
using Gift.Model;

public partial class cn_UserControl_header : System.Web.UI.UserControl
{
    // Fields
     string basicSetXmlPath = "/cn/xml/website_set.xml";
    //protected BrandManager bm = new BrandManager();
    //protected CategoryManager cm = new CategoryManager();
    //protected CustomNavigationManager cnm = new CustomNavigationManager();
    protected InBoxManager ibm = new InBoxManager();
  //  protected KeyWords kw = new KeyWords();
    //protected KeyWordsManager kwm = new KeyWordsManager();

    tbKeywords oKeywords = new tbKeywords();

    readonly DALKeywords theKeywords = new DALKeywords();
     string moduleSetXmlPath = "/cn/xml/module_set.xml";
    readonly DALPinpai thePinpai = new DALPinpai();
    readonly DALClass theClass = new DALClass();
    readonly DALProductclass theProductClass = new DALProductclass();

    // Methods
    protected StringBuilder BrandBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            XmlDocument document = new XmlDocument();
            document.Load(base.Server.MapPath(this.moduleSetXmlPath));
            XmlElement element = document.SelectSingleNode("set/top_brand") as XmlElement;
            if (!(element.GetAttribute("isShow") == "true"))
            {
                return builder;
            }
         //   DataTable brandByLanguage = this.bm.GetBrandByLanguage("zh-CN");
            DataTable dtBrand = thePinpai.GetList(" languages = 1 and isTop = 1 ").Tables[0];
            builder.AppendLine("<div class=\"brand\">");
            builder.AppendLine("<div>");
            builder.AppendLine("<h6>" + element.GetAttribute("title") + "：</h6>");
            foreach (DataRow row in dtBrand.Rows)
            {
                builder.AppendLine(string.Concat(new object[] { "<a href=\"", row["pinpaiUrl"], "\"><img src=\"", row["pinpaiLogo"], "\" alt=\"", row["pinpaiName"], "\" /></a>" }));
            }
            builder.AppendLine("</div>");
            builder.AppendLine("</div>");
        }
        catch
        {
        }
        return builder;
    }

    protected StringBuilder KeyWordsBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            builder.AppendLine("<p>");
            builder.AppendLine("<strong>热门搜索：</strong>");
            DataTable keyWordsByTopAndIsShow = theKeywords.GetList(10, string.Empty, " clickCount DESC ").Tables[0];
            foreach (DataRow row in keyWordsByTopAndIsShow.Rows)
            {
                builder.AppendLine(string.Concat(new object[] { "<a href=\"/cn/goods_list.html?kw=", HttpUtility.UrlEncode(row["keyWord"].ToString()), "\" class=\"red\">", row["keyWord"], "</a>" }));
            }
            builder.AppendLine("</p>");
        }
        catch
        {
        }
        return builder;
    }

    protected StringBuilder MenuNavigationBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable table3;
            XmlDocument document = new XmlDocument();
            document.Load(base.Server.MapPath(this.moduleSetXmlPath));
            XmlElement element = document.SelectSingleNode("set/navigation_brand") as XmlElement;
            //DataTable table = this.cm.GetCategoryByParentIdAndLanguageVersionAndIsShow("00000000-0000-0000-0000-000000000000", "zh-CN", 1);
            DataTable tbHeadMenu = theClass.GetMenuList(1);
           // DataTable table2 = this.cm.GetCategoryByNavigationTypeAndLanguageVersionAndIsShow(0, "zh-CN", 1);
            DataTable tbAllBrand = thePinpai.GetClassBrandList(null).Tables[0];
            builder.AppendLine("<div class=\"menu\">");
            builder.AppendLine("<div class=\"hcategory2\" id=\"categorybox\">");
            builder.AppendLine("<div class=\"navin clearfix\">");
            builder.AppendLine("<div class=\"search_bar clearfix\">");
            builder.AppendLine("<div class=\"hcategory2\" id=\"categorybox2\">");
            builder.AppendLine("<div class=\"cate-tit\" id=\"cate_tit\"><a href=\"/cn/goods_list.html\" title=\"全部商品分类\"><span>全部商品分类</span></a></div>");
            builder.AppendLine("<div class=\"category_bar\" id=\"category_bar\">");
            builder.AppendLine("<div class=\"cate-tb\"></div>");
            builder.AppendLine("<div id=\"cate_bar\">");
            int num = 1;
            foreach (DataRow row in tbHeadMenu.Rows)
            {
                builder.AppendLine("<div class=\"cate_item\">");
                builder.AppendLine("<div class=\"cate_title\">");
                builder.AppendLine(string.Concat(new object[] { "<h3><strong><a href=\"/cn/channel.html?cid=", row["classid"], "\">", row["className"], "</a></strong></h3>" }));
                builder.AppendLine("<em></em>");
                builder.AppendLine("</div>");
                builder.AppendLine("<div class=\"citem top_" + num + "\">");
                builder.AppendLine("<div class=\"citem_left\">");
                builder.AppendLine("<div class=\"all_cate_tit\">全部分类</div>");
                builder.AppendLine("<dl>");
                //table3 = this.cm.GetCategoryByParentIdAndLanguageVersionAndIsShow(row["id"].ToString(), "zh-CN", 1);
                table3 = theProductClass.GetHeadMenuLevel2List(int.Parse(row["Classid"].ToString()));
                foreach (DataRow row2 in table3.Rows)
                {
                    builder.AppendLine(string.Concat(new object[] { "<dt><a href=\"goods_list.html?cid=", row2["classId"], "\">", row2["className"], "</a> </dt>" }));
                    //DataTable table4 = this.cm.GetCategoryByParentIdAndLanguageVersionAndIsShow(row2["id"].ToString(), "zh-CN", 1);
                    DataTable table4 = theProductClass.GetHeadMenuLevel3List(int.Parse(row2["classid"].ToString()));
                    foreach (DataRow row3 in table4.Rows)
                    {
                        builder.AppendLine(string.Concat(new object[] { "<dd><a href=\"/cn/goods_list.html?cid=", row3["classid"], "\">", row3["className"], "</a></dd>" }));
                    }
                }
                builder.AppendLine("</dl>");
                builder.AppendLine("</div>");
                if (element.GetAttribute("isShow") == "true") 
                {
                    builder.AppendLine("<div class=\"citem_right\">");
                    builder.AppendLine("<dl class=\"brands\">");
                    builder.AppendLine("<dt>" + element.GetAttribute("title") + "</dt>");
                    builder.AppendLine("<dd>");
                  //  brandByCategoryIdentity = this.bm.GetBrandByCategoryIdentity(Convert.ToInt32(row["categoryIdentity"]));
                    DataTable brandByCategoryIdentity = thePinpai.GetClassBrandList(row["classid"].ToString()).Tables[0];

                    foreach (DataRow row4 in brandByCategoryIdentity.Rows)
                    {
                        builder.AppendLine(string.Concat(new object[] { "<em><a href=\"/cn/goods_list.html?cid=", row["Classid"], "&bid=", row4["pinpaiId"], "\">", row4["pinpaiName"], "</a></em>" }));
                    }
                    builder.AppendLine("</dd>");
                    builder.AppendLine("</dl>");
                    builder.AppendLine("</div>");
                }
                builder.AppendLine("<span class=\"clear\"></span>");
                builder.AppendLine("</div>");
                builder.AppendLine("</div>");
                num++;
            }
            builder.AppendLine("<div class=\"allcate\"><a href=\"/cn/goods_list.html\">全部商品分类</a></div>");
            builder.AppendLine("<div class=\"catebom\"></div>");
            builder.AppendLine("</div>");
            builder.AppendLine("</div>");
            builder.AppendLine("</div>");
            builder.AppendLine("</div>");
            builder.AppendLine("</div>");
            builder.AppendLine("</div>");
            builder.AppendLine("<ul id=\"navMenu\">");
            builder.AppendLine("<li><a href=\"/cn/index.html\" class=\"home\">首页</a></li>");
            foreach (DataRow row in tbHeadMenu.Rows)
            {
                builder.AppendLine(string.Concat(new object[] { "<li><a href=\"/cn/channel.html?cid=", row["classid"], "\" rel=\"dropmenu", row["ClassId"], "\">", row["className"], "</a></li>" }));
            }

            XmlElement eleValidateCard = document.SelectSingleNode("set/top_validate_card") as XmlElement;
            XmlElement eleTuanShop = document.SelectSingleNode("set/top_tuan_shop") as XmlElement;

            if (eleValidateCard.GetAttribute("isShow") == "true")
            {
                builder.AppendLine(string.Concat(new object[] { "<li><a class=\"last\" href=\"", eleValidateCard.GetAttribute("url"), "\">", eleValidateCard.GetAttribute("title"), "</a></li>" }));
            }

            if (eleTuanShop.GetAttribute("isShow") == "true")
            {
                builder.AppendLine(string.Concat(new object[] { "<li><a class=\"last\" href=\"", eleTuanShop.GetAttribute("url"), "\">", eleTuanShop.GetAttribute("title"), "</a></li>" }));
            }

            //DataTable allCustomNavigation = this.cnm.GetAllCustomNavigation();
            //foreach (DataRow row in allCustomNavigation.Rows)
            //{
            //    builder.AppendLine(string.Concat(new object[] { "<li><a class=\"last\" href=\"", row["url"], "\">", row["title"], "</a></li>" }));
            //}
            //builder.AppendLine("<li><a class=\"last\" href=\"javascript:void();\" onclick=\"art.dialog.tips('体验版此功能未开通！',1.5);\">秒杀团购</a></li>");
            builder.AppendLine("</ul>");
            foreach (DataRow row5 in tbHeadMenu.Rows)
            {
            //    brandByCategoryIdentity = theProductClass.GetHeadMenuLevel2List(Convert.ToInt32(row5["bigClassid"]));
                builder.AppendLine("<div id=\"dropmenu" + row5["ClassId"] + "\" class=\"dropMenu\">");
                builder.AppendLine("<dl>");
                table3 = theProductClass.GetHeadMenuLevel2List(int.Parse(row5["Classid"].ToString()));
                foreach (DataRow row2 in table3.Rows)
                {
                    builder.AppendLine(string.Concat(new object[] { "<dt><a href=\"/cn/goods_list.html?cid=", row2["classid"], "\" class=\"title_menu\">", row2["className"], "</a></dt>" }));
                    DataTable table7 = theProductClass.GetHeadMenuLevel3List(int.Parse(row2["Classid"].ToString()));
                    if (table7.Rows.Count > 0)
                    {
                        builder.AppendLine("<dd>");
                        for (int i = 0; i < table7.Rows.Count; i++)
                        {
                            builder.AppendLine(string.Concat(new object[] { "<a href=\"/cn/goods_list.html?cid=", table7.Rows[i]["Classid"], "\" >", table7.Rows[i]["className"], "</a>" }));
                        }
                        builder.AppendLine("</dd>");
                    }
                    else
                    {
                        builder.AppendLine("<dd style=\"height:1px; overflow:hidden;\"></dd>");
                    }
                }
                builder.AppendLine("</dl>");
                //if (element.GetAttribute("isShow") == "true")
                //{
                //    builder.AppendLine("<div class=\"brand_menu\">");
                //    builder.AppendLine("<p><strong>" + element.GetAttribute("title") + "</strong></p>");
                //    foreach (DataRow row4 in tbAllBrand.Rows)
                //    {
                //        builder.AppendLine(string.Concat(new object[] { "<p><a href=\"/cn/goods_list.html?cid=", row5["BigClassId"], "&bid=", row4["pinpaiId"], "\">", row4["pinpaiName"], "</a></p>" }));
                //    }
                //    builder.AppendLine("</div>");
                //}
                builder.AppendLine("</div>");
            }
            builder.AppendLine("<div class=\"clear\"></div>");
            builder.AppendLine("</div>");
        }
        catch
        {
        }
        return builder;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!(base.IsPostBack || (base.Request.QueryString["kw"] == null)))
        {
            this.oKeywords.keyword =  base.Request.QueryString["kw"];
            this.oKeywords.languages = 1;
            this.theKeywords.Add(oKeywords);
        }
    }

    protected StringBuilder UserBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            string attribute = "";
            XmlDocument document = new XmlDocument();
            document.Load(base.Server.MapPath(this.basicSetXmlPath));
            attribute = ((XmlElement)document.SelectSingleNode("set/cn_enterprise_sort_name")).GetAttribute("value");
            builder.AppendLine("<div class=\"wel\">");
            builder.AppendLine("<div class=\"width1000\">");
            builder.AppendLine("<p class=\"top_function\">");
            builder.AppendLine("<a href=\"/cn/member_center/my_order.html\" class=\"order\">我的订单</a><a href=\"/cn/shopping_cart.html\" class=\"shopping_car\">我的购物车</a><a href=\"/cn/help_center/help_list.html\" class=\"help\">帮助中心</a><a href=\"/cn/news_center/news_list.html\" class=\"into\">资讯中心</a><a href=\"/cn/recruitment/recruitment_list.html\" class=\"human\">人才招聘</a><a href=\"/cn/online_message.html\" class=\"omessage\">在线留言</a>");
            builder.AppendLine("<p id=\"puser\">");
            if (base.Session["user"] != null)
            {
                int inBoxNoIsReadByAddressee = this.ibm.GetInBoxNoIsReadByAddressee((base.Session["user"] as User).UserName);
                builder.AppendLine(string.Concat(new object[] { "您好，<span class=\"red\">", (base.Session["user"] as User).UserName, "</span>，欢迎光临 <strong>", attribute, "</strong>！&nbsp;<a href=\"/cn/member_center/my_order.html\">[&nbsp;会员中心&nbsp;]</a>&nbsp;<a class=\"mail\" href=\"/cn/member_center/in_box.html\">(<strong>", inBoxNoIsReadByAddressee, "</strong>)</a>&nbsp;<a href=\"/cn/user_exit.ashx\">[&nbsp;退出&nbsp;]</a>" }));
            }
            else
            {
                builder.AppendLine("您好，欢迎光临 <strong>" + attribute + "</strong>！&nbsp;<a href=\"/cn/login.html?TB_iframe=true&height=370&width=640\" class=\"thickbox\">[<span class=\"red\">登录</span></a> /&nbsp;<a href=\"/cn/register.html?TB_iframe=true&height=370&width=640\" class=\"thickbox\">注册]</a>");
            }
            builder.AppendLine("<p>");
            builder.AppendLine("</div>");
            builder.AppendLine("</div>");
        }
        catch
        {
        }
        return builder;
    }

}